package com.grupoconstruobras.sigo.exhumaciones.ui.validator;

import java.io.Serializable;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class RequiredValidator implements Validator, Serializable {
	
	//@ManagedProperty("#{msgVal}")
	private ResourceBundle bundle;
	
	public RequiredValidator() {
		FacesContext fc = FacesContext.getCurrentInstance();
		bundle = fc.getApplication().evaluateExpressionGet(fc, "#{msgVal}", ResourceBundle.class);
	}

	@Override
	public void validate(FacesContext fc, UIComponent component, Object value)
			throws ValidatorException {
		
		/*if(value == null) {
			String msg = bundle.getString("constraint.not_null");
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, name + msg, null);
			fc.addMessage(component.getClientId(), message);
			throw new ValidatorException(message);
		}*/
		
		String svalue = (String) value;
		if(svalue.equals("-1")) {
			String msg = bundle.getString("constraint.not_null");
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, null);
			fc.addMessage(component.getClientId(), message);
			throw new ValidatorException(message);
		}		
	}

}
